package ai;

public class AnalyseBoard {

	int[][] currentBoard_refactored; //Inherited and defined by Move.class
	
	
	/* INFO ABOUT THE FOLLOWING CODE
	 * The following code is various checks for how many crosses 
	 * or circles there is in row.
	 * 
	 * All the check returns an array of length 4
	 * 
	 * The methods definetively have some potential for improvement. 
	 * Feel free
	 * 
	 * The methods might lack some description, but I dont know how 
	 * to clarify them any further. Just try to read/study them
	 */
	
	
	//----------------------------------------------------------------|
	//			Analyse the board to find 3- or 4 in-a-row			  |
	//----------------------------------------------------------------|
	
	/* analyseHoriz()
	 *This method checks for how many symbols of type [int c] there is 
	 *in a horizontal line:
	 *<---------[int x, int y]----------->
	 */
	protected int[] analyseHoriz(int x, int y, int c, int howManyInRow) {
		int inRow = 0;
		int returnX=-1;
		int returnY=-1;
		int[] returnThis = new int[4];

		for(int i=-3;i<=3;y++){
			try{
				if(currentBoard_refactored[y][x+i]==c){
					inRow++;
					if(inRow==howManyInRow){
						try{
							if(currentBoard_refactored[y][x+i+1]==-1){
								returnY=y;
								returnX=(x+i+1);
								break;
							} else if (currentBoard_refactored[y][x+i-1]==-1){
								returnY=y;
								returnX=(x+i-1);
								break;
							}
						}catch(Exception e){
							try{
								if (currentBoard_refactored[y][x+i-1]==-1){
									returnY=y;
									returnX=(x+i-1);
									break;
								}
							}catch(Exception e2){}
						}
					}
				}
				if(currentBoard_refactored[y][x+i]!=c){
					inRow = 0;
				}
			} catch(Exception e){}
		}
		returnThis[0]=returnX;
		returnThis[1]=returnY;
		returnThis[2]=inRow;
		returnThis[3]=0;

		return returnThis;
	}

	/* analyseVert()
	 *This method checks for how many symbols of type [int c] there is 
	 *in a vertical line:
	 * 		^
	 * 		|
	 * 		|
	 * [int x, int y]
	 * 		|
	 * 		|
	 * 		v
	 */
	protected int[] analyseVert(int x, int y, int c, int howManyInRow) {
		int inRow = 0;
		int returnX=-1;
		int returnY=-1;
		int[] returnThis = new int[4];
		for(int i=-3;i<=3;y++){ //we have to try/catch in case of NullPointerException
			try{
				if(currentBoard_refactored[y+i][x]==c){
					inRow++;
					if(inRow==howManyInRow){
						try{
							if(currentBoard_refactored[y+i+1][x]==-1){
								returnY=(y+i+1);
								returnX=x;
								break;
							} else if (currentBoard_refactored[y+i-1][x]==-1){
								returnY=(y+i-1);
								returnX=x;
								break;
							}
						}catch(Exception e){
							try{
								if (currentBoard_refactored[y+i-1][x]==-1){
									returnY=(y+i-1);
									returnX=x;
									break;
								}
							} catch(Exception e2){}
						}
					}
				}
				if(currentBoard_refactored[y][x+i]!=c){
					inRow = 0;
				}
			} catch(Exception e){}
		}
		returnThis[0]=returnX;
		returnThis[1]=returnY;
		returnThis[2]=inRow;
		returnThis[3]=0;

		return returnThis;
	}

	/* analyseDiag1()
	 *This method checks for how many symbols of type [int c] there is 
	 *in a diagonal line:
	 *		  /\
	 *	     /
	 *		/
	 *[int x, int y]
	 *    /
	 *   /
	 *.\/
	 */
	protected int[] analyseDiag1(int x, int y, int c, int howManyInRow){
		int inRow = 0;
		int returnX=-1;
		int returnY=-1;
		int[] returnThis = new int[4];
		int onTop = 1;

		for(int i=-3;i<=3;y++){
			try{
				if(currentBoard_refactored[y+i][x-i]==c){
					inRow++;
					if(inRow==howManyInRow){
						try{
							if(currentBoard_refactored[y+i+1][x-i+1]==-1){
								returnY=(y+i+1);
								returnX=(x-i+1);
								onTop = -1;
								break;
							} else if (currentBoard_refactored[y+i-1][x-i-1]==-1){
								returnY=(y+i-1);
								returnX=(x-i-1);
								break;
							}
						}catch(Exception e){
							try{
								if (currentBoard_refactored[y+i-1][x-i-1]==-1){
									returnY=(y+i-1);
									returnX=(x-i-1);
									break;
								}
							}catch(Exception e2){}
						}
					}
				}
				if(currentBoard_refactored[y+i][x-i]!=c){
					inRow = 0;
				}
			} catch(Exception e){}
		}
		returnThis[0]=returnX;
		returnThis[1]=returnY;
		returnThis[2]=inRow;
		returnThis[3]=onTop;

		return returnThis;
	}

	/* analyseDiag2()
	 *This method checks for how many symbols of type [int c] there is 
	 *in a diagonal line:
	 *./\
	 *	 \
	 *	  \	
	 *[int x, int y]
	 *		\
	 *		 \
	 *		  \/
	 */
	protected int[] analyseDiag2(int x, int y, int c, int howManyInRow){
		int inRow = 0;
		int returnX=-1;
		int returnY=-1;
		int[] returnThis = new int[4];
		int onTop = -1;

		for(int i=-3;i<=3;y++){
			try{
				if(currentBoard_refactored[y-i][x+i]==c){
					inRow++;
					if(inRow==howManyInRow){
						try{
							if(currentBoard_refactored[y-i+1][x+i+1]==-1){
								onTop = 1;
								returnY=(y-i+1);
								returnX=(x+i+1);
								break;
							} else if (currentBoard_refactored[y-i-1][x+i-1]==-1){
								returnY=(y-i-1);
								returnX=(x+i-1);
								break;
							}
						}catch(Exception e){
							try{
								if (currentBoard_refactored[y-i-1][x+i-1]==-1){
									returnY=(y-i-1);
									returnX=(x+i-1);
									break;
								}
							}catch(Exception e2){}
						}
					}
				}
				if(currentBoard_refactored[y-i][x+i]!=c){
					inRow = 0;
				}
			} catch(Exception e){}
		}
		returnThis[0]=returnX;
		returnThis[1]=returnY;
		returnThis[2]=inRow;
		returnThis[3]=onTop;
		return returnThis;
	}

}
